type TypeListItem = {
  label: string;
  value: number | string;
  color?: string;
  [key: string]: any;
};

type TypeObject = {
  [key: string]: TypeListItem;
};

export type OperationType =
  | 'APPROVE'
  | 'ADD'
  | 'UPDATE'
  | 'DELETE'
  | 'VIEW'
  | 'DETAIL'
  | 'EXPORT'
  | 'IMPORT'
  | 'PRINT'
  | 'EDIT'
  | 'PWD';

/** 操作类型 */
export const OperationTypeList = {
  ADD: {
    label: '新增',
    value: 'add',
  },
  UPDATE: {
    label: '修改',
    value: 'update',
  },
  DELETE: {
    label: '删除',
    value: 'delete',
  },
  VIEW: {
    label: '查看',
    value: 'view',
  },
  DETAIL: {
    label: '详情',
    value: 'detail',
  },
  EXPORT: {
    label: '导出',
    value: 'export',
  },
  IMPORT: {
    label: '导入',
    value: 'import',
  },
  PRINT: {
    label: '打印',
    value: 'print',
  },
  EDIT: {
    label: '编辑',
    value: 'edit',
  },
  APPROVE: {
    label: '审批',
    value: 'approve',
  },
  PWD: {
    label: '修改密码',
    value: 'pwd',
  },
};

/** 是否 */
export const OperationTypeBoolean = {
  YES: {
    label: '是',
    value: 1,
  },
  NO: {
    label: '否',
    value: 0,
  },
};

/** 启用/禁用状态 */
export const EnableStatusType = {
  ENABLED: {
    label: '启用',
    value: 1,
    status: 'success',
    color: '#52c41a',
  },
  DISABLED: {
    label: '禁用',
    value: 0,
    status: 'error',
    color: '#ff4d4f',
  },
};

/** 启用/禁用操作文本 */
export const EnableActionText = {
  ENABLE: '启用',
  DISABLE: '禁用',
  ENABLE_SUCCESS: '启用成功',
  DISABLE_SUCCESS: '禁用成功',
  ENABLE_FAILED: '启用失败',
  DISABLE_FAILED: '禁用失败',
};

/** 是否 */
export const DeviceLocationType = {
  CITY: {
    label: '城市',
    value: 0,
    color: '#108ee9',
  },
  SITE: {
    label: '场地',
    value: 1,
    color: '#87d068',
  },
  POINT: {
    label: '点位',
    value: 2,
    color: '#2db7f5',
  },
};

/** 统计设备类型 */
export const DeviceListType = {
  DIRECT: {
    label: '直连设备',
    value: 1,
    color: '#00C5C3',
  },
  GATEWAY: {
    label: '网关设备',
    value: 2,
    color: '#2F82F5',
  },
  SUB_DEVICE: {
    label: '网关子设备',
    value: 3,
    color: '#FC9560',
  },
};

/** 统计设备类型 */
export const DeviceCreateTypeList = {
  MANUAL: {
    value: '0',
    label: '人工注册',
  },
  AUTO: {
    value: '1',
    label: '自动注册',
  },
};

/** 统计设备状态 */
export const DeviceListStatus = {
  PENDING: {
    label: '待激活',
    value: 0,
    color: '#1CA5FC',
    status: 'default',
  },
  ONLINE: {
    label: '在线',
    value: 1,
    color: '#00C5C3',
    status: 'success',
  },
  OFFLINE: {
    label: '离线',
    value: 2,
    color: '#FDA87C',
    status: 'error',
  },
  UNKNOWN: {
    label: '未知',
    value: 3,
    color: '#7E8A9B',
    status: 'warning',
  },
};

/** 产品节点类型 */
export const ProductNodeType = {
  DIRECT: {
    value: 1,
    label: '直连设备',
  },
  GATEWAY: {
    value: 2,
    label: '网关设备',
  },
  SUB_DEVICE: {
    value: 3,
    label: '子设备',
  },
};

/** 告警级别 */
export const AlarmLevelEnum = {
  NORMAL: {
    label: '普通',
    value: 0,
    status: 'default',
    color: '#7E8A9B',
  },
  SERIOUS: {
    label: '严重',
    value: 1,
    status: 'error',
    color: '#ff4d4f',
  },
  URGENT: {
    label: '紧急',
    value: 2,
    status: 'warning',
    color: '#FDA87C',
  },
};

/** 告警类型 */
export const AlarmTypeEnum = {
  DEVICE: {
    label: '设备告警',
    value: 1,
    status: 'processing',
    color: '#1CA5FC',
  },
};

/** 联网方式 */
export const NetworkModeType = {
  CELLULAR: {
    value: '蜂窝',
    label: '蜂窝',
  },
  ETHERNET: {
    value: '以太',
    label: '以太',
  },
  NB: {
    value: 'NB',
    label: 'NB',
  },
  WIFI: {
    value: 'WIFI',
    label: 'WIFI',
  },
  OTHER: {
    value: '其他',
    label: '其他',
  },
};

/** 功能类型 */
export const FunctionTypeList = {
  PROPERTIES: {
    value: 'properties',
    label: '属性',
  },
  EVENTS: {
    value: 'events',
    label: '事件',
  },
  SERVICES: {
    value: 'services',
    label: '服务',
  },
};

/*功能类别 */
export const FunctionCatList = {
  STANDARD: {
    value: 'st',
    label: '标准',
  },
  USER: {
    value: 'u',
    label: '自定义',
  },
  SYSTEM: {
    value: 's',
    label: '系统',
  },
};

/** 读写类型 */
export const ReadWriteTypeList = {
  READ_WRITE: {
    value: 'rw',
    label: '读写',
  },
  READ_ONLY: {
    value: 'r',
    label: '只读',
  },
};

/** 数据类型 */
export const DataTypeList = {
  INT32: {
    label: 'int32(整数型)',
    value: 'int32',
  },
  INT64: {
    label: 'int64(长整数型)',
    value: 'int64',
  },
  FLOAT: {
    label: 'float(单精度浮点型)',
    value: 'float',
  },
  DOUBLE: {
    label: 'double(双精度浮点型)',
    value: 'double',
  },
  ENUM: {
    label: 'enum(枚举)',
    value: 'enum',
  },
  BOOL: {
    label: 'bool(布尔)',
    value: 'bool',
  },
  STRING: {
    label: 'string(字符串)',
    value: 'string',
  },
  DATE: {
    label: 'date(时间)',
    value: 'date',
  },
  ARRAY: {
    label: 'array(数组)',
    value: 'array',
  },
  FILE: {
    label: 'file(文件)',
    value: 'file',
  },
  PASSWORD: {
    label: 'password(密码)',
    value: 'password',
  },
  GEOPOINT: {
    label: 'geopoint(地理位置)',
    value: 'geopoint',
  },
};

/** 服务执行状态 */
export const ServiceStatusList = {
  PENDING: {
    label: '待下发',
    value: 0,
    color: '#1CA5FC',
  },
  SENT: {
    label: '已下发',
    value: 1,
    color: '#FDA87C',
  },
  EXECUTED: {
    label: '已执行',
    value: 2,
    color: '#00C5C3',
  },
};

/** 转换成valueEnum类型 */
export const typeToValueEnum = (typeList: TypeObject) => {
  const result: Record<number | string, { text: string; [key: string]: any }> = {};
  Object.values(typeList).forEach(item => {
    result[item.value] = { text: item.label };
  });
  return result;
};
/** 根据值获取枚举对象的 label */
export const getEnumLabel = (
  enumObj: Record<string, TypeListItem>,
  value: string | number,
  defaultValue: string | number = ''
) => {
  for (const key in enumObj) {
    if (Object.hasOwn(enumObj, key)) {
      const item = enumObj[key];
      if (item.value == value) {
        return item.label;
      }
    }
  }
  return defaultValue;
};

/** 根据值获取枚举对象的 label */
export const getEnumItem = (
  enumObj: Record<string, TypeListItem>,
  value: string | number,
  defaultValue = {} as TypeListItem
) => {
  for (const key in enumObj) {
    if (Object.hasOwn(enumObj, key)) {
      const item = enumObj[key];

      if (item.value == value) {
        return item;
      }
    }
  }
  return defaultValue;
};

/** 对象转数组 */
export const objToArray = (enumObj: Record<string, TypeListItem>) => {
  // 创建一个空数组
  const arr: TypeListItem[] = [];

  // 遍历枚举对象
  for (const key in enumObj) {
    if (Object.hasOwn(enumObj, key)) {
      const item = enumObj[key];
      // 将枚举对象中的每个属性转换为键值对对象，并添加到数组中
      arr.push(item);
    }
  }

  return arr;
};
